﻿//------------------------------------------------------------------------------------------------------
// <summary>
//     The following code is allowed to be used inernally in Pfizer China only, 
//     you are not permitted neither to use the contents nor disclose it in any
//     manner to third parties.
// </summary>
// <copyright file="UserCookie.cs" company="Pfizer China">
//     Copyright (c) Pfizer China. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using SystemFrameWork;
using SystemFrameWork.Enums;

namespace DutyWeb
{
    /// <summary>
    /// the cookies manager
    /// </summary>
    public class UserCookie
    {
        #region construction

        /// <summary>
        /// Get the single object
        /// </summary>
        public static UserCookie CurrentCookie
        {
            get
            {
                return currentCookie();
            }
        }

        /// <summary>
        /// Create object follow singleton pattern
        /// </summary>
        /// <returns></returns>
        private static UserCookie currentCookie()
        {
            if (_instance == null)
            {
                _instance = new UserCookie();
            }
            return _instance;
        }

        private static UserCookie _instance;

        /// <summary>
        /// confine the UserCookie to a singleton type
        /// </summary>
        private UserCookie()
        { }

        public TimeSpan CookieTimeSpan
        {
            get;
            set;
        }

        #endregion

        #region properties



        #endregion

        #region methods

        /// <summary>
        /// set a value to a cookie
        /// </summary>
        /// <param name="cookieName">cookie name</param>
        /// <param name="key">key</param>
        /// <param name="value">value</param>
        /// <param name="ts">time span</param>
        /// <param name="page">the responded page</param>
        private void SetValue(string cookieName, string key, string value, TimeSpan ts)
        {
            try
            {
                HttpCookie cookie = new HttpCookie(cookieName);
                DateTime dt = DateTime.Now;
                cookie.Expires = dt.Add(ts);
                cookie.Values.Add(key, value);
                HttpContext.Current.Response.Cookies.Add(cookie);
            }
            catch (Exception ex)
            {
                throw new BaseException(ex.Message);
            }
        }

        /// <summary>
        /// get values from a specific cookie
        /// </summary>
        /// <param name="cookieName"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        private string GetValue(string cookieName, string key)
        {
            try
            {
                if (System.Web.HttpContext.Current.Request.Cookies[cookieName] != null)
                {
                    string temp = Convert.ToString(System.Web.HttpContext.Current.Request.Cookies[cookieName].Values[key]);
                    return temp;
                }
                else
                {
                    return string.Empty;
                }
            }
            catch (Exception ex)
            {
                throw new BaseException(ex.Message);
            }
        }

        #endregion
    }
}
